import axios from 'axios'
import { Loading } from 'element-ui'

let loadingInstance
const options = {    //全局加载特效配置
  lock: true,
  text: '加载中.....',
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.2)',
}


axios.defaults.timeout = 10000
axios.defaults.baseURL = '/api'

axios.interceptors.request.use((config) => {
  loadingInstance = Loading.service(options)  //加载特效

  const token = window.sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = window.sessionStorage.getItem('token')
  }
  return config
})

axios.interceptors.response.use((res) => {
  if (res.status == 200) {
    loadingInstance.close()  //关闭全局加载特效
    return res.data
  }
})

export default axios
